package com.example.calendarapp;

import android.support.v7.app.ActionBarActivity;
import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.Toast;

/**
 * This class consists of several methods that
 * inflates the menu and specifies what happens 
 * when the user clicks on the Home/Up button. 
 *
 * @author Emilio Celis-Aguirre
 * @author Jeremy Henderson
 * @author Felipe Luna
 * @author Nathan Whitaker
 * @author Xiaoxu Zhang
 */
public class MainActivity extends ActionBarActivity implements OnNavigationListener{
	private String[] fragmentStrings = {"Month", "Week", "Day"};
	FragmentManager fm;
	FragmentTransaction transaction;
	MonthFragment fragMonth;
	WeekFragment fragWeek;
	DayFragment fragDay;
	
	/**
	* This method gets a MonthFragment, WeekFragment, and DayFragment.
	* @param savedInstanceState
	*/
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		ArrayAdapter<String> list = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, fragmentStrings);
		list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		getActionBar().setListNavigationCallbacks(list, this);
		
		fragMonth = new MonthFragment();
		fragWeek = new WeekFragment();
		fragDay = new DayFragment();
		fm = getFragmentManager();
		transaction = fm.beginTransaction();
		transaction.replace(R.id.frag, fragMonth);
		transaction.commitAllowingStateLoss();
		
		
	}

	/**
	* When a navigation item is selected, this method either gets the MonthFragment,
	* WeekFragment, or the DayFragment. 
	* @param arg0
	* @param arg1
	* @return false
	*/
	
	@Override
	public boolean onNavigationItemSelected(int arg0, long arg1) {
		fm = getFragmentManager();
		switch(arg0)
		{
		case 0:
			if(fm.findFragmentById(R.id.frag) instanceof MonthFragment)
				break;
			transaction = fm.beginTransaction();
			transaction.replace(R.id.frag, fragMonth);
			transaction.addToBackStack(null);
			transaction.commitAllowingStateLoss();
			break;
		case 1:
			if(fm.findFragmentById(R.id.frag) instanceof WeekFragment)
				break;
			transaction = fm.beginTransaction();
			transaction.replace(R.id.frag, fragWeek);
			transaction.addToBackStack(null);
			transaction.commitAllowingStateLoss();
			break;
		case 2:
			if(fm.findFragmentById(R.id.frag) instanceof DayFragment)
				break;
			transaction = fm.beginTransaction();
			transaction.replace(R.id.frag,fragDay);
			transaction.addToBackStack(null);
			transaction.commitAllowingStateLoss();
			break;
		}
		return false;
	}

	/**
	 * This method inflates the menu. It adds items to the action bar if it is present.
	 * @param menu
	 * @return true
	 */
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	
	/**
	 * This method handles the action bar when clicked. The action bar will automatically
	 * handle clicks on the Home/Up button, so long as you specify a parent activity in 
	 * AndroidManifest.xml.
	 * @param item
	 * @return true		Returns true if id == R.id.action_settings
	 * @return super.onOptionsItemSelected(item)
	 */
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	/**
	 * This method returns true or the keyCode and event.
	 * @param keyCode 
	 * @param event
	 * @return true
	 * @return super.onKeyDown(keyCode, event)
	 */
	
	@Override 
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{ 
	    if (keyCode == KeyEvent.KEYCODE_BACK && getFragmentManager().getBackStackEntryCount() != 0)
	    { 
	    	FrameLayout container = (FrameLayout) findViewById(R.id.frag);
	    	container.removeAllViews();
	    	getFragmentManager().popBackStack();
	    	return true;
	 
	    } 
	    else
	    	return super.onKeyDown(keyCode, event);
	} 

}
